<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/8/22
 * Time: 21:46
 */

namespace app\home\controller;
use app\home\model\Course;
use app\home\model\WechatUser;
use app\home\model\CourseSign;
use app\home\model\CourseEvaluate;
use com\wechat\TPWechat;
use think\Config;
use app\home\model\Pic;
/**
 * Class Evaluate
 * @package app\home\controller
 * 评价系统
 */

class Evaluate extends Base
{
    /**
     * 评价页面
     */
    public function index(){
        $userId = session('userId');
        $id = input('id');  // 课表 id
        // 老师 未认证 未关注 或者为分配学员  无权限
        $status = CourseSign::where(['openid' => $userId,'c_id' => $id])->value('status');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0 || ($status != 1 && $status != 2 || empty($id))){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $end_time = Course::where('id',$id)->value('end_time');
        if (($end_time-1800) >= time()){
            // 还不能评价
            $this->assign('is',1);
        }else{
            $this->assign('is',0);
        }
        // 是否已经评价
        $res = CourseEvaluate::where(['openid' => $userId,'c_id' => $id])->value('content');
        if ($res){
            // 已评论
            $this->redirect('Evaluate/sign',['id' => $id]);
        }else{
            // 未评论
            $this->assign('id',$id);
            return $this->fetch();
        }
    }
    /**
     * 发布
     */
    public function save(){
        $userId = session('userId');
        $data = input('post.');  // 内容 con  pic score id
        $this->jssdk();
        $Wechat = new TPWechat(Config::get('party'));
        $tem = [];
        if (!empty($data['pic'])){
            foreach ($data['pic'] as $value){
                $pic = $Wechat->getMedia($value);
                $url = "./uploads/download/images/".time().uniqid().".jpg";
                $myfile = fopen($url, "w+");
                fwrite($myfile, $pic);
                fclose($myfile);
                $Picture = new Pic();
                $id = $Picture->save(['path' => substr($url,1),'status' => 0,'create_time' => time()]);
                array_push($tem,$id);
            }
        }
        $teacher_id = Course::where(['id' => $data['id']])->value('teacher_id');
        $info = array(
            'openid' => $userId,
            'c_id' => $data['id'],
            't_id' => $teacher_id,
            'content' => $data['con'],
            'images' => json_encode($tem),
            'score' => $data['score'],
            'create_time' => time(),
        );
        $Evaluate = new CourseEvaluate();
        $res = $Evaluate->save($info);
        if ($res){
            WechatUser::where('openid',$userId)->setInc('score',2);
            return $this->success('提交成功');
        }else{
            return $this->error('提交失败');
        }
    }
    /**
     * 评价列表
     */
    public function sign(){
        $id = input('id');  // 课表 id
        $Model = new CourseEvaluate();
        $list = $Model->where(['c_id' => $id,'score' => ['>=',2]])->field('openid,content,images,score,create_time')->order('create_time','desc')->limit(15)->select();
        foreach ($list as $value) {
            //用户信息处理
            $value['time'] = date('Y-m-d', $value['create_time']);
            $User = WechatUser::where(['openid' => $value['openid']])->field('name,headimgurl,school_id')->find();
            $value['name'] = $User['name'];
            $value['school'] = get_school($User['school_id']);
            $value['headimgurl'] = $User['headimgurl'];
            $path = [];
            //图片处理
            if ($value['images']){
                $pic = json_decode($value['images'],true);
                foreach ($pic as $item){
                    $path[] = "/DDDeducation/public".Pic::where('id',$item)->value('path');
                }
            }
            $value['img'] = $path;
        }
        $this->assign('list',$list);
        $this->assign('id',$id);
        return $this->fetch();
    }
    /**
     * 评价更多
     */
    public function more(){
        $id = input('id');  // 课表 id
        $len = input('length');  // 长度
        $Model = new CourseEvaluate();
        $list = $Model->where(['c_id' => $id,'score' => ['>=',2]])->field('openid,content,images,score,create_time')->order('create_time','desc')->limit($len,15)->select();
        foreach ($list as $value) {
            //用户信息处理
            $value['time'] = date('Y-m-d', $value['create_time']);
            $User = WechatUser::where(['openid' => $value['openid']])->field('name,headimgurl,school_id')->find();
            $value['name'] = $User['name'];
            $value['school'] = get_school($User['school_id']);
            $value['headimgurl'] = $User['headimgurl'];
            $path = [];
            //图片处理
            if ($value['images']){
                $pic = json_decode($value['images'],true);
                foreach ($pic as $item){
                    $path[] = "/DDDeducation/public".Pic::where('id',$item)->value('path');
                }
            }
            $value['img'] = $path;
        }
        if ($list){
            return $this->success('加载成功','',$list);
        }else{
            return $this->error('加载失败');
        }
    }
}